package com.hzeport.bgs22emduty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.SystemConfig;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.ScreenNotificationDTO;
import com.hzeport.bgs22emduty.dto.ScreenNotificationSaveDTO;
import com.hzeport.bgs22emduty.entity.Appendix;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.outbound.AppendixRepository;
import com.hzeport.bgs22emduty.outbound.ScreenNotificationRepository;
import com.hzeport.bgs22emduty.vo.file.FileVO;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import com.hzeport.bgs22emduty.entity.ScreenNotification;
import com.hzeport.bgs22emduty.outbound.db.ScreenNotificationRepositoryImpl;
import com.hzeport.bgs22emduty.service.ScreenNotificationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2023-02-14 09:39:44
 */
@Service
@Slf4j
public class ScreenNotificationServiceImpl extends BaseServiceImpl<ScreenNotificationRepositoryImpl, ScreenNotification> implements ScreenNotificationService {

    @Resource
    private ScreenNotificationRepository screenNotificationRepository;

    @Resource
    private LoginCompoentService loginCompoentService;
    @Resource
    private SystemConfig systemConfig;
    @Resource
    private AppendixRepository appendixRepository;


    @Override
    public Object page(ScreenNotificationDTO dto) {
        UserInfoDetailVO userInfoDetailVO = loginCompoentService.getUserInfo();
        LambdaQueryWrapper<ScreenNotification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userInfoDetailVO.getCustomscode()),ScreenNotification::getCustomId,userInfoDetailVO.getCustomscode());
        queryWrapper.like(StringUtils.isNotBlank(dto.getKeyWord()),ScreenNotification::getTitle,dto.getKeyWord());
        if (!CollectionUtils.isEmpty(dto.getList()) && StringUtils.isNotBlank(dto.getList().get(0))){
            LocalDateTime startLocalDateTime = LocalDate.parse(dto.getList().get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
            queryWrapper.ge(ScreenNotification::getCreateTime,startLocalDateTime);
        }
        if (!CollectionUtils.isEmpty(dto.getList()) && StringUtils.isNotBlank(dto.getList().get(1))){
            LocalDateTime endLocalDateTime = LocalDate.parse(dto.getList().get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay().plusDays(1);
            queryWrapper.le(ScreenNotification::getCreateTime,endLocalDateTime);
        }
        queryWrapper.orderByDesc(ScreenNotification::getCreateTime);
        Page<ScreenNotification> page = screenNotificationRepository.page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper);
        return page;
    }

    @Override
    public BaseResult save(ScreenNotificationSaveDTO dto) throws Exception {
//        if (CollectionUtils.isEmpty(dto.getFiles())){
//            throw new Exception("必须传递pdf文件！");
//        }
//        FileVO file=dto.getFiles().get(0);
//        if (!file.getCurrentFileName().endsWith(".pdf")){
//            throw new Exception("文件后缀名必须为pdf！");
//        }
        UserInfoDetailVO userInfoDetailVO = loginCompoentService.getUserInfo();
        ScreenNotification screenNotification = new ScreenNotification();
        BeanUtil.copyProperties(dto,screenNotification);
        screenNotification.setCustomId(userInfoDetailVO.getCustomscode());
        screenNotification.setCustomName(userInfoDetailVO.getCustomsName());
        screenNotificationRepository.save(screenNotification);
        List<FileVO> files = dto.getFiles();
        files.forEach(fileVO -> {
            LambdaUpdateWrapper<Appendix> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Appendix::getId,fileVO.getId()).set(Appendix::getRelevanceId,screenNotification.getId());
            appendixRepository.update(wrapper);
        });
        return BaseResult.ok("新增成功");
    }
}
